********************************************************************************
*Project:  	Swiss Trade During the Covid-19 Pandemic: An Early Appraisal	
*Authors:	KB, SL, VP, PW											
*This File: Figure 5
*Version: 	vFinal						
*Date: 		2020-09-01				
********************************************************************************



********************************************************************************
********************************************************************************
*Comparison with great depression, suspension of  minimum exchange rate peg
********************************************************************************
********************************************************************************

use "${root}\workfiles\trade_2002_2020_M_analysis_wide.dta", replace

*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
*CHOOSE TIMING PARAMETERS & EVENTS
**********************************

*CHOOSE MAIN SAMPLE
*(1) Rohdaten 
*(21) Ländertotal
*(22) Kontinenttotal 
*(31) EU-Total (inkl. GB) 
*(32) EU+EFTA-Total (inkl. GB)
*(33) Eurozone-Total 
*(51) Kat. 12: Warenart-Total
*(52) Kat. 2: Warenart-Total
*(53) Kat. 3: Warenart-Total 
*(54) Kat. 4: Warenart-Total
*(55) Kat. 5: Warenart-Total
*(56) Kat. 6: Warenart-Total 
*(99) Welt-Total

local main_sample=99

*CHOOSE SUB SAMPLE VARIABLE
*waren_art_kat12 
*waren_art_kat6 
*waren_art_kat5 
*waren_art_kat4 
*waren_art_kat3 
*waren_art_kat2
*no
local sub_sample "no"

*CHOOSE TIMING PARAMETERS
*Number of months before event
local lag_past=3

*Number of Months after event
local lag_future=11

*Rolling Window
local window_roll=0

*CHOOSE EVENTS
*Insolvenz Lehman & Rettung UBS
local date08="2008m9"

*Aufhebung Mindestkurs
local date15="2015m1"

*Lockdown
local date20="2020m3"
*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!


*Drop irrelevant observations
keep if SAMPLE==`main_sample'

if "`sub_sample'"=="no" {
	local sub_sample="SAMPLE" 
}

di in red "`sub_sample'"
levelsof `sub_sample', local(v_ss)
di in red "`v_ss'"
foreach value of local v_ss  {
	
	preserve 
	
	*Keep relevant observations: 
	keep if `sub_sample'==`value'
	
	*TSSET
	sort jahr monat
	gen seq=_n
	tsset seq
	
	
	*SET EVENTS
	*Insolvenz Lehman
	sum seq if datum==tm(`date08')
	local start_08=`r(mean)'
	di `start_08'

	*Aufhebung Mindestkurs
	sum seq if datum==tm(`date15')
	local start_15=`r(mean)'
	di `start_15'

	*Lockdown
	sum seq if datum==tm(`date20')
	local start_20=`r(mean)'
	di `start_20'

	*seq_shock: Months relative to event
	gen seq_shock=.
	replace seq_shock=0 if seq==`start_08'
	replace seq_shock=seq-`start_08'
	replace seq_shock=0 if seq==`start_15'
	replace seq_shock=seq-`start_15' if jahr>=2014 & jahr<=2018 
	replace seq_shock=0 if seq==`start_20'
	replace seq_shock=seq-`start_20' if jahr>=2019 & jahr<=2020 

	*Total Trade in Mia
	gen total_chf_mia=import_chf_mia+export_chf_mia
	gen total_tonnen=import_tonnen+export_tonnen
	
	*Trade in reference month prior to event 
	foreach ht in import export total {
	    foreach unit in chf_mia tonnen {
			forvalues v=0(1)11 {
				local v12=12+`v'
				local v24=24+`v'
				local v36=36+`v'
				local v48=48+`v'
				local v60=60+`v'
				
				gen `ht'_`unit'_RY_l`v'=l`v12'.`ht'_`unit' if  seq_shock<=11
				replace `ht'_`unit'_RY_l`v'=l`v24'.`ht'_`unit' if seq_shock>=12 & seq_shock<=23
				replace `ht'_`unit'_RY_l`v'=l`v36'.`ht'_`unit' if seq_shock>=24 & seq_shock<=35
				replace `ht'_`unit'_RY_l`v'=l`v48'.`ht'_`unit' if seq_shock>=36 & seq_shock<=47
				replace `ht'_`unit'_RY_l`v'=l`v48'.`ht'_`unit' if seq_shock>=36 & seq_shock<=47
				replace `ht'_`unit'_RY_l`v'=l`v60'.`ht'_`unit' if seq_shock>=48 & seq_shock<=59
				replace `ht'_`unit'_RY_l`v'=l`v60'.`ht'_`unit' if seq_shock>=48 & seq_shock<=59

			}
		}
	}
	
	*Trade in previous months
	foreach ht in import export total {
		foreach unit in chf_mia tonnen {	
			forvalues v=1(1)11 {
				gen `ht'_`unit'_l`v'=l`v'.`ht'_`unit'
			}
		}
	}
	
	*Deviation
	foreach ht in import export total {
	    foreach unit in chf_mia tonnen {
			gen `ht'_`unit'_m3_E=`ht'_`unit'+`ht'_`unit'_l1+`ht'_`unit'_l2
			gen `ht'_`unit'_m3_RY=`ht'_`unit'_RY_l0+`ht'_`unit'_RY_l1+`ht'_`unit'_RY_l2
			gen `ht'_`unit'_dev_m3=`ht'_`unit'_m3_E-`ht'_`unit'_m3_RY
		}
		gen `ht'_chf_dev100_m3=100*(`ht'_chf_mia_m3_E-`ht'_chf_mia_m3_RY)/`ht'_chf_mia_m3_RY
		gen `ht'_tonnen_dev100_m3=100*(`ht'_tonnen_m3_E-`ht'_tonnen_m3_RY)/`ht'_tonnen_m3_RY
	}

	*Deviation from trade in reference year (as percent) 
	foreach ht in import export total {
	     foreach unit in chf_mia tonnen {
			gen `ht'_dev100_`unit'_RY=(`ht'_`unit'/`ht'_`unit'_RY_l0)*100-100
		 }
	}	
	
		
	*Trade volume during month_t0 
	foreach event in 08 15 20 {
		foreach ht in import export total {
		     foreach unit in chf_mia tonnen {
				gen `ht'_`unit'_t0_`event'_t=`ht'_`unit' if seq==`start_`event''
				egen `ht'_`unit'_t0_`event'=mode(`ht'_`unit'_t0_`event'_t)
			 }
		}
	}
	drop *_t

	*Deviation from trade in month_t0 (as percent)
	foreach ht in import export total {
	    foreach unit in chf_mia tonnen {
			gen `ht'_dev100_`unit'_t0=(`ht'_`unit'/`ht'_`unit'_t0_08)*100
			replace `ht'_dev100_`unit'_t0=(`ht'_`unit'/`ht'_`unit'_t0_15)*100 if jahr>=2014 & jahr<=2018
			replace `ht'_dev100_`unit'_t0=(`ht'_`unit'/`ht'_`unit'_t0_20)*100 if jahr>=2019 & jahr<=2020 
		}
	}	
		
	foreach event in 08 15 20 {
	    foreach unit in chf_mia tonnen {
			foreach ht in import export total {		
		    
				if "`unit'"=="chf_mia" {
				 local unitS "mia"
				 di in red "`unitS'"
				}
				
				if "`unit'"=="tonnen" {
				 local unitS "ton"
				 di in red "`unitS'"
				}
				
				*Cumulative trade (in bn) during period under consideration
				gen `ht'_CUM`unitS'_`event'=`ht'_`unit' if seq==`start_`event''-`lag_past' 
				replace `ht'_CUM`unitS'_`event'=l1.`ht'_CUM`unitS'_`event'+`ht'_`unit' if seq>`start_`event''-`lag_past'
			
				*Cumulative trade (in bn) during reference period (Lag12) under consideration
				gen `ht'_REF`unitS'_`event'=l12.`ht'_`unit' if seq==`start_`event''-`lag_past' 
				replace `ht'_REF`unitS'_`event'=l1.`ht'_REF`unitS'_`event'+l12.`ht'_`unit' if seq>`start_`event''-`lag_past'

				*Cumulative trade difference (in bn) versus reference period (Lag12)
				gen `ht'_CUMdiff_`unit'_`event'=`ht'_CUM`unitS'_`event'-`ht'_REF`unitS'_`event' if seq>=`start_`event''-`lag_past' 
				
				*Cumulative trade (in bn) during period under consideration + ROLLING WINDOW (window_roll)
				gen `ht'_rCUM`unitS'_`event'=`ht'_`unit' if seq==`start_`event''-`lag_past'-`window_roll' 
				replace `ht'_rCUM`unitS'_`event'=l1.`ht'_rCUM`unitS'_`event'+`ht'_`unit' if seq>`start_`event''-`lag_past'-`window_roll' 

				*Cumulative trade (in bn) during reference period (Lag12) under consideration + ROLLING WINDOW (window_roll)
				gen `ht'_rREF`unitS'_`event'=l12.`ht'_`unit' if seq==`start_`event''-`lag_past'-`window_roll'  
				replace `ht'_rREF`unitS'_`event'=l1.`ht'_rREF`unitS'_`event'+l12.`ht'_`unit' if seq>`start_`event''-`lag_past'-`window_roll'  
				
				*Deviation from cumulative trade during period under consideration vs. reference period (as percent)
				gen `ht'_CUM100_`unit'_`event'=100*`ht'_CUM`unitS'_`event'/`ht'_REF`unitS'_`event' if seq>=`start_`event''-`lag_past'

				*Deviation from ROLLING cumulative trade during period under consideration vs. reference period (as percent)
				gen `ht'_rCUM100_`unit'_`event'=100*`ht'_rCUM`unitS'_`event'/`ht'_rREF`unitS'_`event' if seq>=`start_`event''-`lag_past' 

				*Normalized deviation from cumulative trade during period under consideration vs. reference period (as percent)
				gen `ht'_CUM100XT_`unit'_`event'=`ht'_CUM100_`unit'_`event'  if seq==`start_`event''
				egen `ht'_CUM100X_`unit'_`event'=mode(`ht'_CUM100XT_`unit'_`event') 
				gen `ht'_CUM100N_`unit'_`event'=100*(`ht'_CUM100_`unit'_`event'/`ht'_CUM100X_`unit'_`event') if seq>=`start_`event''-`lag_past'
				
				drop `ht'_CUM100XT_`unit'_`event' `ht'_CUM100X_`unit'_`event'
				
				*Normalized deviation from cumulative trade during period under consideration vs. reference period (as percent) + ROLLING WINDOW (window_roll)
				gen `ht'_rCUM100XT_`unit'_`event'=`ht'_rCUM100_`unit'_`event'  if seq==`start_`event''
				egen `ht'_rCUM100X_`unit'_`event'=mode(`ht'_rCUM100XT_`unit'_`event') 
				gen `ht'_rCUM100N_`unit'_`event'=100*(`ht'_rCUM100_`unit'_`event'/`ht'_rCUM100X_`unit'_`event') if seq>=`start_`event''-`lag_past'-`window_roll'  
				
				drop `ht'_rCUM100XT_`unit'_`event' `ht'_rCUM100X_`unit'_`event'
			
			}
				
			
			*Ratio (cum. Exports/ cum. Imports) and share of cumulative exports of cumulative total trade
			gen ratio_CUM`unitS'_exim_`event'=100*(export_CUM`unitS'_`event'/import_CUM`unitS'_`event')
			gen share_CUM`unitS'_ex_`event'=100*(export_CUM`unitS'_`event'/(import_CUM`unitS'_`event'+export_CUM`unitS'_`event'))
			
			*Ratio (cum. Exports/ cum. Imports) and share of cumulative exports of cumulative total trade (ROLLING WINDOW)
			gen ratio_rCUM`unitS'_exim_`event'=100*(export_rCUM`unitS'_`event'/import_rCUM`unitS'_`event')
			gen share_rCUM`unitS'_ex_`event'=100*(export_rCUM`unitS'_`event'/(import_rCUM`unitS'_`event'+export_rCUM`unitS'_`event'))
			
		}
	}
	
	if "`sub_sample'"=="SAMPLE" {
		save "${root}/temp/trade_M_`main_sample'_comparison.dta", replace
		restore, not
	}
	
	if "`sub_sample'"!="SAMPLE" {
		foreach var in export import ratio share total {
			di in red "`value'"
			rename `var'* `var'*_`value'
		}
		keep  jahr monat seq seq_shock datum land_id 							///
		`sub_sample'  export* import* ratio* share* total*
		
		rename `sub_sample'  `sub_sample'_`value'
		
		save "${root}/temp/trade_M_`main_sample'_`sub_sample'_`value'_comparison.dta", replace
		restore
	}
	
}

if "`sub_sample'"=="SAMPLE" {
	use "${root}/temp/trade_M_`main_sample'_comparison.dta", clear
}

if "`sub_sample'"!="SAMPLE" {
	local i=0
	levelsof `sub_sample', local(v_ss)
	foreach value of local v_ss  {
		local i=`i'+1
		
		if `i'==1 {
			use "${root}/temp/trade_M_`main_sample'_`sub_sample'_`value'_comparison.dta", clear
		}
		
		else {
			merge 1:1 jahr monat datum seq seq_shock  land_id  					///
			using "${root}/temp/trade_M_`main_sample'_`sub_sample'_`value'_comparison.dta"
		assert _merge==3
		drop _merge
		}
	}

save "${root}/temp/trade_M_`main_sample'_`sub_sample'_comparison.dta", replace
	
}
	
global LP=-`lag_past'
global LF=`lag_future'


//TOTAL TRADE 
use "${root}/temp/trade_M_99_comparison.dta", clear
*Comparison -- Great Recession 08/09 vs Lockdown 2020:
*Deviation of cumulative exports/imports (in bn) of reference period.
graph twoway	(line export_CUMdiff_chf_mia_08  seq_shock if seq_shock>=$LP & seq_shock<=$LF & jahr<2014, lcolor(gs10) lwidth(medthick) lpattern(longdash)) ///
				(line import_CUMdiff_chf_mia_08  seq_shock if seq_shock>=$LP & seq_shock<=$LF & jahr<2014, lcolor(gs10) lwidth(medthick) lpattern(shortdash)) ///
				(line export_CUMdiff_chf_mia_20  seq_shock if seq_shock>=$LP & seq_shock<=$LF & jahr>2018, lcolor(gs2) lwidth(medthick) lpattern(longdash)) ///
				(line import_CUMdiff_chf_mia_20  seq_shock if seq_shock>=$LP & seq_shock<=$LF & jahr>2018, lcolor(gs2) lwidth(medthick) lpattern(shortdash)), ///
				xscale(range(-3 11)) xlabel(#17) yline(0, lcolor(gs12)) xline(0, lcolor(maroon)) ///
				ytitle("Billion CHF") xtitle("Months")  ///
				text(-20 -0.5 "Event", place(e) orient(vertical) color(maroon))  ///
				legend(order (- "Event: Lehman Brothers 09/2008 "  1 "Exports" 2 "Imports" ///
				- "Event: Swiss Lockdown 03/2020" 3 "Exports" 4 "Imports")		///
				colgap(*0.5) keygap(*0.5) symxsize(*0.75) cols(3)) ///
				scheme(s1color)
graph export "${root}\figures\FIG4a_exp_imp_M_GR_COVID19_CUMDIFF_CHF.png", replace	
graph export "${root}\figures\FIG4a_exp_imp_M_GR_COVID19_CUMDIFF_CHF.pdf", replace	




*Comparison -- Great Recession 08/09 vs Lockdown 2020:
*Percent Deviation  of exports / imports (in CHF) of reference period.
graph twoway	(line export_dev100_chf_mia_RY  seq_shock if seq_shock>=$LP & seq_shock<=$LF & jahr<2014, lcolor(gs10) lwidth(medthick) lpattern(longdash)) ///
				(line import_dev100_chf_mia_RY  seq_shock if seq_shock>=$LP & seq_shock<=$LF & jahr<2014, lcolor(gs10) lwidth(medthick) lpattern(shortdash)) ///
				(line export_dev100_chf_mia_RY  seq_shock if seq_shock>=$LP & seq_shock<=$LF & jahr>2018, lcolor(gs2) lwidth(medthick) lpattern(longdash)) ///
				(line import_dev100_chf_mia_RY  seq_shock if seq_shock>=$LP & seq_shock<=$LF & jahr>2018, lcolor(gs2) lwidth(medthick) lpattern(shortdash)), ///
				xscale(range(-3 11)) xlabel(#17) yline(0, lcolor(gs12)) ///
				ytitle("Percent") xtitle("Months") xline(0, lcolor(maroon)) ///
				text(-28 -0.5 "Event", place(e) orient(vertical) color(maroon))  ///
				legend(order (- "Event: Lehman Brothers 09/2008 "  1 "Exports" 2 "Imports" ///
				- "Event: Swiss Lockdown 03/2020" 3 "Exports" 4 "Imports")		///
				colgap(*0.5) keygap(*0.5) symxsize(*0.75) cols(3)) ///
				scheme(s1color)
graph export "${root}\figures\FIG4b_exp_imp_M_GR_COVID19_DMONTH_PERCENT_CHF.pdf", replace	
graph export "${root}\figures\FIG4b_exp_imp_M_GR_COVID19_DMONTH_PERCENT_CHF.png", replace	
